/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import myUtils.page.Result;
import org.hibernate.Hibernate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import po.Mail;
import po.User;

/**
 *
 * @author sony
 */
public class MailDaoImpl extends HibernateDaoSupport implements MailDao {

    public void save(Mail mail) throws Exception {
        this.getHibernateTemplate().save(mail);
    }

    public Mail getMail(int mailId) {
        return (Mail) getHibernateTemplate().get(Mail.class, mailId);
    }

    public void deleteMail(int mailId) throws Exception {
        Object p = getHibernateTemplate().load(Mail.class, mailId);
        this.getHibernateTemplate().delete(p);
    }

    public Result getReceiveMails(int id, int page) {
        String queryString = "from Mail where userByToId.id=" + id + " and isread in (0,3,10,13) order by time desc";
        List<Mail> mails = getHibernateTemplate().find(queryString);
        return new Result(page, mails);
    }

    public Result getSendBox(int id, int page) {
        String queryString = "from Mail where userByFromId.id=" + id + "  and isread in (0,2,10,12) order by time desc";
        List<Mail> mails = getHibernateTemplate().find(queryString);
        return new Result(page, mails);
    }

    public void updateMail(Mail mail) {
        getHibernateTemplate().update(mail);
    }

}
